const express = require("express");
const router = express.Router();
const {asyncHandler} = require("../middleWare/getSentResult");
const {getR,setR,delR,caseTime,addOne} = require('../server/redisServe')
const commentServer = require('../server/commentServer');
const messageServer = require('../server/messageServer');
const {postNum} = require("../routerConfig");
const path = require("path");
const fs = require("fs")

router.get('/comment/:_id',asyncHandler(async (req,res,next)=>{

    const id = req.params._id;
    const query = req.query;
    const limit = query.limit || 10;
    const skip = query.skip || 1 ;
     const Cresult = await  getR(6,req.originalUrl) 
     if(Cresult) {
         return JSON.parse(Cresult);
     }   
     const result =  await commentServer.getComment(id,limit,skip);
     await setR(6,req.originalUrl,JSON.stringify(result),{
        EX:caseTime
    });
    return result;

} ))


router.post('/comment/:_id',asyncHandler(async (req,res,next)=>{
    const ip  = req.ip + "comment";
    if( await isIp(ip)) return "请勿频繁提交"
    
    const blogId = req.params._id;
    const {id,text,name,content,blogTitle} = req.body
    if(!blogId) return '_id 不能为 null';

    const val = await getR(5,id);
    if(val == text ){
        if(name === ''  || content === '') return "昵称或评论不能为空!"; 
        const result = await commentServer.addComment( blogId,name,content,blogTitle);
        await delR(5,id);
        await addOne(ip);
        return result;
    }else{
        return "验证码错误";
    }

   
    
}))



router.delete('/comment/:_id',asyncHandler(async(req,res,next)=>{
    const _id = req.params._id;
    const result = await commentServer.delComment(_id);
    return result;
}))


router.get('/message',asyncHandler(async(req,res,next)=>{

    const {limit, skip} = req.query;
    const Cresult = await getR(7,req.originalUrl); 
    if(Cresult) {
        return JSON.parse(Cresult)
    }   
    const result = await messageServer.getMessage(limit,skip);
    await setR(7,req.originalUrl,JSON.stringify(result),{
        EX:caseTime
    });
    return result;


}))
router.post('/message',asyncHandler(async(req,res,next)=>{
    

    const ip  = req.ip + "message";
    if( await isIp(ip)) return "请勿频繁提交"
    
    const {id,text,name,content} = req.body
    if(!id) return 'id 不能为 null';
    const val = await getR(5,id);
    if(val == text ){
        if(name === ''  || content === '') return "昵称或评论不能为空!"; 
        const result = await  messageServer.addMessage(name,content);
        delR(5,id);
        await addOne(ip);
        return result;
    }else{
        return "验证码错误";
    }
}))


router.get('/img',async(req,res,next)=>{
    
    
    let r =  Math.random()*10
    
   
    const ram = (Math.floor(r)) == 0 ? 1 : Math.floor(r) ;
    const result = fs.readFileSync( path.resolve(__dirname,`../public/assets/banner/banner${ram}.jpg`)); //,{encoding:"binary"}
    res.header("Cache-Control",`max-age=0`);
    res.header("Content-Type","image/jpg");
    res.status(200).send(result);
    
})

async function isIp(ip){

    const Cip = await getR(5,ip);
    if(!Cip){
       await setR(5,ip,"1", {
            EX: caseTime * 2
        });
        return false;
    }else if(Cip > postNum) {
        return true;
    }

}

module.exports = router;